home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -seriously_amiga- / programming / other / gtlayout / source / ltp_newmenu.c < prev    next >
C/C++ Source or Header  |  1999-04-19  |  4KB  |  207 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1998 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. /*****************************************************************************/
  15.  
  16. #include <exec/memory.h>
  17.  
  18. #include <dos/dos.h>    /* For AmigaDOS error definitions */
  19.  
  20. #include <clib/alib_protos.h>    /* For NewList */
  21.  
  22. /*****************************************************************************/
  23.  
  24. #include "Assert.h"
  25.  
  26. #ifdef DO_MENUS    /* Support code */
  27.  
  28.     /* LTP_NewMenu():
  29.      *
  30.      *    Create a new menu, based on the Screen and Font given.
  31.      */
  32.  
  33. RootMenu *
  34. LTP_NewMenu(struct Screen *Screen,struct TextAttr *TextAttr,struct Image *AmigaGlyph,struct Image *CheckGlyph,LONG *ErrorPtr)
  35. {
  36.     LONG Error;
  37.  
  38.     if(Screen)
  39.     {
  40.         APTR Pool;
  41.  
  42.             // Wrap all the allocations into a pool
  43.  
  44.         if(Pool = AsmCreatePool(MEMF_ANY | MEMF_PUBLIC | MEMF_CLEAR,1024,1024,SysBase))
  45.         {
  46.             struct RootMenu *Root;
  47.  
  48.                 // Create the root
  49.  
  50.             if(Root = (struct RootMenu *)AsmAllocPooled(Pool,sizeof(struct RootMenu),SysBase))
  51.             {
  52.                 Root->Pool = Pool;
  53.  
  54.                 if(TextAttr)
  55.                     Root->TextAttr = TextAttr;
  56.                 else
  57.                     Root->TextAttr = Screen->Font;
  58.  
  59.                     // Open the menu font
  60.  
  61.                 if(Root->Font = OpenFont(Root->TextAttr))
  62.                 {
  63.                         // Get the drawing information
  64.  
  65.                     if(Root->DrawInfo = GetScreenDrawInfo(Screen))
  66.                     {
  67.                             // Fill in the dummy rastport
  68.  
  69.                         InitRastPort(&Root->RPort);
  70.  
  71.                         Root->RPort.BitMap = Screen->RastPort.BitMap;
  72.  
  73.                         SetFont(&Root->RPort,Root->Font);
  74.  
  75.                                 // Get the text rendering pen
  76.  
  77.                         if(Root->DrawInfo->dri_Version < 2)
  78.                             Root->TextPen = Root->DrawInfo->dri_Pens[DETAILPEN];
  79.                         else
  80.                             Root->TextPen = Root->DrawInfo->dri_Pens[BARDETAILPEN];
  81.  
  82.                                 // Something to remember
  83.  
  84.                         Root->Screen = Screen;
  85.  
  86.                             // Let's hope it won't grow in the future
  87.  
  88.                         CopyMem(Root->TextAttr,&Root->BoldAttr,sizeof(struct TTextAttr));
  89.  
  90.                             // Make it boldface
  91.  
  92.                         Root->BoldAttr.tta_Style |= FSF_BOLD;
  93.  
  94.                             // Initialize the lists
  95.  
  96.                         NewList((struct List *)&Root->MenuList);
  97.                         NewList((struct List *)&Root->ItemList);
  98.  
  99.                             // Get the glyph widths
  100.  
  101.                         if(CheckGlyph)
  102.                         {
  103.                             GetAttr(IA_Width,    CheckGlyph,&Root->CheckWidth);
  104.                             GetAttr(IA_Height,    CheckGlyph,&Root->CheckHeight);
  105.                         }
  106.                         else
  107.                         {
  108.                                 // No glyph is provided, use the default values
  109.  
  110.                             if(V39)
  111.                             {
  112.                                 struct Image *Glyph;
  113.  
  114.                                 if(Glyph = NewObject(NULL,SYSICLASS,
  115.                                     SYSIA_DrawInfo,         Root->DrawInfo,
  116.                                     SYSIA_Which,            MENUCHECK,
  117.                                     SYSIA_ReferenceFont,    Root->Font,
  118.                                 TAG_DONE))
  119.                                 {
  120.                                     GetAttr(IA_Width,    Glyph,&Root->CheckWidth);
  121.                                     GetAttr(IA_Height,    Glyph,&Root->CheckHeight);
  122.  
  123.                                     DisposeObject(Glyph);
  124.                                 }
  125.                             }
  126.  
  127.                             if(!Root->CheckWidth)
  128.                                 Root->CheckWidth = 15;
  129.  
  130.                             if(!Root->CheckHeight)
  131.                                 Root->CheckHeight = 8;
  132.                         }
  133.  
  134.                         if(AmigaGlyph)
  135.                         {
  136.                             GetAttr(IA_Width,    AmigaGlyph,&Root->AmigaWidth);
  137.                             GetAttr(IA_Height,    AmigaGlyph,&Root->AmigaHeight);
  138.                         }
  139.                         else
  140.                         {
  141.                                 // No glyph is provided, use the default values
  142.  
  143.                             if(V39)
  144.                             {
  145.                                 struct Image *Glyph;
  146.  
  147.                                 if(Glyph = NewObject(NULL,SYSICLASS,
  148.                                     SYSIA_DrawInfo,         Root->DrawInfo,
  149.                                     SYSIA_Which,            AMIGAKEY,
  150.                                     SYSIA_ReferenceFont,    Root->Font,
  151.                                 TAG_DONE))
  152.                                 {
  153.                                     GetAttr(IA_Width,    Glyph,&Root->AmigaWidth);
  154.                                     GetAttr(IA_Height,    Glyph,&Root->AmigaHeight);
  155.  
  156.                                     DisposeObject(Glyph);
  157.                                 }
  158.                             }
  159.  
  160.                             if(!Root->AmigaWidth)
  161.                                 Root->AmigaWidth = 23;
  162.  
  163.                             if(!Root->AmigaHeight)
  164.                                 Root->AmigaHeight = 8;
  165.                         }
  166.  
  167.                             // Establish default menu item height
  168.  
  169.                         Root->ItemHeight = Root->RPort.TxHeight;
  170.  
  171.                         if(Root->CheckHeight > Root->ItemHeight)
  172.                             Root->ItemHeight = Root->CheckHeight;
  173.  
  174.                         if(Root->AmigaHeight > Root->ItemHeight)
  175.                             Root->ItemHeight = Root->AmigaHeight;
  176.  
  177.                         Root->ItemHeight += 2;
  178.  
  179.                         return(Root);
  180.                     }
  181.                     else
  182.                         Error = ERROR_NO_FREE_STORE;
  183.  
  184.                     CloseFont(Root->Font);
  185.                 }
  186.                 else
  187.                     Error = ERROR_OBJECT_NOT_FOUND;
  188.             }
  189.             else
  190.                 Error = ERROR_NO_FREE_STORE;
  191.  
  192.             AsmDeletePool(Pool,SysBase);
  193.         }
  194.         else
  195.             Error = ERROR_NO_FREE_STORE;
  196.     }
  197.     else
  198.         Error = ERROR_REQUIRED_ARG_MISSING;
  199.  
  200.     if(ErrorPtr)
  201.         *ErrorPtr = Error;
  202.  
  203.     return(NULL);
  204. }
  205.  
  206. #endif    /* DO_MENUS */
  207.